package xwbank.jmeter5.scripts.sofa.service;

import com.alipay.sofa.rpc.config.ApplicationConfig;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.config.RegistryConfig;
import xwbank.jmeter5.scripts.sofa.LoanResultNoticeTest;

public class CmpServiceSingleton {
    private  CmpService cmpService = null;
    private static class SingletonHolder extends SofaEnvRegisterLockUtil{
        private static CmpServiceSingleton uniqueInstance = new CmpServiceSingleton();
    }
    private CmpServiceSingleton(){
        SofaEnvRegisterLockUtil.difEnvRegisterLock.lock();
        try{
            System.setProperty("com.alipay.instanceid","VIIZTWQ0JVKC");
            System.setProperty("com.alipay.env","shared");
            System.setProperty("com.antcloud.antvip.endpoint","10.80.131.43");
            RegistryConfig registryConfig = new RegistryConfig();
            registryConfig
                    .setProtocol("dsr")
                    .setSubscribe(true)
                    .setConnectTimeout(30*1000);
            ApplicationConfig applicationConfig = new ApplicationConfig();
            applicationConfig.setAppName("cmp-gateway-consumer" + System.currentTimeMillis());
            String interfaceId = CmpService.class.getName();
            ConsumerConfig<CmpService> consumerConfig = new ConsumerConfig<CmpService>()
                    .setInterfaceId(interfaceId)
                    .setProtocol("bolt")
                    .setApplication(applicationConfig)
                    .setRegistry(registryConfig)
                    .setConnectTimeout(30 * 1000)
                    .setRepeatedReferLimit(-1)
                    .setTimeout(30 * 1000);
            setCmpService(consumerConfig.refer());
        }finally {
            SofaEnvRegisterLockUtil.difEnvRegisterLock.unlock();
        }
    }
    public static CmpServiceSingleton getInstance(){return LoanResultNoticeTest.SingletonHolder.uniqueInstance;}

    public CmpService getCmpService(){return cmpService;}

    public void setCmpService(CmpService cmpService){this.cmpService = cmpService;}
}
